﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HKEA.CMS.Core;
using HKEA.CMS.Core.BLL;
using HKEA.CMS.Core.Models;
using HKEA.CMS.Core.BLL.Mtbl;

namespace WebApplication
{
    public partial class Search : BasePage
    {
        public int mark_a = 0, mark_b = 0,mark_c=0,mark_d=0;
        #region ProductCat
        mtblProductCat entProductCat = new mtblProductCat();
        mtblProductCatBLL bllProductCat = mtblProductCatBLL.GetInstance();
        List<mtblProductCat> listProductCat = new List<mtblProductCat>();
        #endregion
        #region Attrbute
        mtblAttribute entAttribute = new mtblAttribute();
        mtblAttributeBLL bllAttribute = mtblAttributeBLL.GetInstance();
        List<mtblAttribute> listAttribute = new List<mtblAttribute>();
        #endregion 
        #region Job Nature
        mtblJobNature entJobNature = new mtblJobNature();
        mtblJobNatureBLL bllJobNature = mtblJobNatureBLL.GetInstance();
        List<mtblJobNature> listJobNature = new List<mtblJobNature>();
        #endregion
        #region BusinessNature
        mtblBusinessNature entBusinessNature = new mtblBusinessNature();
        mtblBusinessNatureBLL bllBusinessNature = mtblBusinessNatureBLL.GetInstance();
        List<mtblBusinessNature> listBusinessNature = new List<mtblBusinessNature>();
        #endregion
        #region Area
        mtblArea entArea = new mtblArea();
        mtblAreaBLL bllArea = mtblAreaBLL.GetInstance();
        List<mtblArea> listArea = new List<mtblArea>();
        mtblCountry entCountry = new mtblCountry();
        mtblCountryBLL bllCountry = mtblCountryBLL.GetInstance();
        List<mtblCountry> listCountry = new List<mtblCountry>();
        #endregion
        #region MembershipStatus
        mtblMembershipStatu entMemShipState = new mtblMembershipStatu();
        mtblMembershipStatusBLL bllMemShipState = mtblMembershipStatusBLL.GetInstance();
        List<mtblMembershipStatu> ListMemShipState = new List<mtblMembershipStatu>();
        #endregion
        #region mtblCustType
        mtblCustType entCustType = new mtblCustType();
        mtblCustTypebBLL bllCustType = mtblCustTypebBLL.GetInstance();
        List<mtblCustType> ListCustType = new List<mtblCustType>();
        #endregion
        #region Committee Position
        mtblCommPosition entCommPosition = new mtblCommPosition();
        mtblCommPositionBLL bllcommPosition = mtblCommPositionBLL.GetInstance();
        List<mtblCommPosition> listCommPosition = new List<mtblCommPosition>();
        #endregion
        #region entCommType
        mtblCommType entCommType = new mtblCommType();
        mtblCommTypeBLL bllCommType = mtblCommTypeBLL.GetInstance();
        List<mtblCommType> ListCommType = new List<mtblCommType>();
        #endregion
        #region entSalutation
        mtblSalutation entSalutation = new mtblSalutation();
        mtblSalutationBLL bllSalutaion = mtblSalutationBLL.GetInstance();
        List<mtblSalutation> ListSalutation = new List<mtblSalutation>();
        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {               
                AreaInfoBound();
                ProductCatBound();
                AttributeBound();
                JobNatureBound();
                BusinessNature();
                AreaDataBount();
                MembershipStatusBound();
                CommitteeTypeBound();
                SalutationBound();
                CustTypeBound();
                CommPositionBound();
            }
        }
        protected void SalutationBound() {
            ListSalutation = bllSalutaion.GetListByENT(entSalutation);
            Salutation.DataSource = ListSalutation;
            Salutation.SelectedIndex = entSalutation.SalutationUID;
            Salutation.DataBind();
            Salutation.Items.Insert(0,"");
            Salutation.Text = "";

        }
        protected void AreaInfoBound() {
            listArea = bllArea.GetListByENT(entArea);
            AreaInfo.DataSource = listArea;
            AreaInfo.SelectedIndex = entArea.AreaUID; 
            AreaInfo.DataBind();
            AreaInfo.Items.Insert(0,"");
            CountryInfoBound(0);
            AreaInfo.Text = "";
        }
        protected void CustTypeBound() {
            ListCustType = bllCustType.GetListByENT(entCustType);
            CustomerType.DataSource = ListCustType;
            CustomerType.SelectedIndex = entCustType.CustTypeUID;            
            CustomerType.DataBind();
            CustomerType.Items.Insert(0,"");
            CustomerType.Text = "";
        }
        protected void CommitteeTypeBound() {
            ListCommType = bllCommType.GetListByENT(entCommType);
            CommitteeType.DataSource = ListCommType;
            CommitteeType.SelectedIndex = entCommType.CommTypeUID;
            CommitteeType.DataBind();
            CommitteeType.Items.Insert(0,"");
            CommitteeType.Text = "";
        }
        protected void MembershipStatusBound() {
            ListMemShipState = bllMemShipState.GetListByENT(entMemShipState);
            MembershipStatus.DataSource = ListMemShipState;
            MembershipStatus.SelectedIndex = entMemShipState.MemStatusUID;
            MembershipStatus.DataBind();
            MembershipStatus.Items.Insert(0,"");
            MembershipStatus.Text = "";
        }
        protected void CommPositionBound() {
            listCommPosition = bllcommPosition.GetListByENT(entCommPosition);
            CommitteePosition.DataSource = listCommPosition;
            CommitteePosition.SelectedIndex = entCommPosition.CommPositionUID;
            CommitteePosition.DataBind();
            CommitteePosition.Items.Insert(0,"");
            CommitteePosition.Text = "";
        }
        protected void CountryInfoBound(int val) {
            if (val != 0)
            {
                entCountry.AreaUID = val;
                listCountry = bllCountry.GetListByENT(entCountry);
                CountryInfo.DataSource = listCountry;
                CountryInfo.DataBind();
            }
            else {
                CountryInfo.ClearSelection();
                CountryInfo.Items.Insert(0, "");
            }
        }
        protected void AreaInfo_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (AreaInfo.SelectedValue != "")
            {
                int dro = Convert.ToInt32(AreaInfo.SelectedValue);
                CountryInfoBound(dro);
            }
            else {
                CountryInfoBound(0);
            }
        }
        protected void BusinessNature() {
            listBusinessNature = bllBusinessNature.GetListByENT(entBusinessNature);
            Business_Nature.DataSource = listBusinessNature;
            Business_Nature.DataBind();
            
        }
        protected void JobNatureBound() {
            listJobNature = bllJobNature.GetListByENT(entJobNature);
            JobNature.DataSource = listJobNature;
            JobNature.DataBind();
        }
        protected void AttributeBound() {
            listAttribute = bllAttribute.GetListByENT(entAttribute);            
            Attribute.DataSource = listAttribute;
            Attribute.DataBind();
        }
        protected void ProductCatBound()
        {
            listProductCat = bllProductCat.GetListByENT(entProductCat);
            List<mtblProductCat> obj_A = listProductCat.Where(p => p.ProductCatID.Contains("A")).ToList();
            List<mtblProductCat> obj_B = listProductCat.Where(p => p.ProductCatID.Contains("B")).ToList();
            List<mtblProductCat> obj_C = listProductCat.Where(p => p.ProductCatID.Contains("C")).ToList();
            List<mtblProductCat> obj_D = listProductCat.Where(p => p.ProductCatID.Contains("D")).ToList();
            List<mtblProductCat> obj_E = listProductCat.Where(p => p.ProductCatID.Contains("E")).ToList();
            //var newList = new List<List<mtblProductCat>> { obj_A, obj_B, obj_C, obj_D, obj_E };

            IList<List<mtblProductCat>> newList = new List<List<mtblProductCat>>();
            newList.Add(obj_A);
            newList.Add(obj_B);
            newList.Add(obj_C);
            newList.Add(obj_D);
            newList.Add(obj_E);


            ProductCategory.DataSource = newList;
            ProductCategory.DataBind();
        }

        protected void ProductCategory_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            List<mtblProductCat> list = new List<mtblProductCat>();
            foreach (var item in e.Item.DataItem as List<mtblProductCat>)
            {
                mtblProductCat entProductCat=new mtblProductCat();
                entProductCat.ProductCatUID=item.ProductCatUID;
                entProductCat.ProductCatID=item.ProductCatID;
                entProductCat.ProductCat_Chi=item.ProductCat_Chi;
                entProductCat.ProductCat_Eng=item.ProductCat_Eng;
                list.Add(entProductCat);            
            }

            Repeater CategoryList = e.Item.FindControl("CategoryList") as Repeater;
            if (list != null)
            {
                CategoryList.DataSource = list;
                CategoryList.DataBind();
            }
        }

        protected void AreaDataBount() {
            listCountry = bllCountry.GetListByENT(entCountry);
            List<mtblCountry> obj_A = listCountry.Where(p => p.AreaUID == 1).ToList();
            List<mtblCountry> obj_B = listCountry.Where(p => p.AreaUID == 2).ToList();
            List<mtblCountry> obj_C = listCountry.Where(p => p.AreaUID == 3).ToList();
            List<mtblCountry> obj_D = listCountry.Where(p => p.AreaUID == 4).ToList();
            List<mtblCountry> obj_E = listCountry.Where(p => p.AreaUID == 5).ToList();
            List<mtblCountry> obj_F = listCountry.Where(p => p.AreaUID == 6).ToList();
            List<mtblCountry> obj_G = listCountry.Where(p => p.AreaUID == 7).ToList();

            IList<List<mtblCountry>> newList = new List<List<mtblCountry>>();
            newList.Add(obj_D);
            newList.Add(obj_A);
            newList.Add(obj_C);
            newList.Add(obj_G);
            newList.Add(obj_B);
            newList.Add(obj_E);
            newList.Add(obj_F);
            

            Area.DataSource = newList;
            Area.DataBind();

        }
        protected void Area_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            List<mtblCountry> list = new List<mtblCountry>();
            foreach (var item in e.Item.DataItem as List<mtblCountry>) {
                mtblCountry entCountr = new mtblCountry();
                entCountr.AreaUID = item.AreaUID;
                entCountr.CountryUID = item.CountryUID;
                entCountr.Country_Chi = item.Country_Chi;
                entCountr.Country_Eng = item.Country_Eng;
                list.Add(entCountr);
            }

            Repeater Country = e.Item.FindControl("Country") as Repeater;
            if (list != null) {
                Country.DataSource = list;
                Country.DataBind();
            }
        }

    }
}